package com.typing.battle.activity;

import com.typing.battle.service.ITypingBattlePollingService;
import com.typing.battle.service.TypingBattlePollingService;
import com.typing.battle.util.PreferenceMgr;
import com.typing.battle.util.ServerComunicationUtil;
import com.typing.battle.value.ServerComunicationDTO;

import com.typing.battle.R;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.TabHost;


public class TypingBattleMainActivity extends TabActivity {
	public ServiceConnection conn=null;
	private PreferenceMgr mgr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mgr = PreferenceMgr.getInstance(this);
        final TabHost tabHost = getTabHost();
        
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("", getResources().getDrawable(R.drawable.friends_icon))
                .setContent(new Intent(this, TypingBattleFriendListActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("", getResources().getDrawable(R.drawable.chat_icon))
                .setContent(new Intent(this, TypingBattleChatRoomListActivity.class)));
        /*tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("", getResources().getDrawable(R.drawable.timeattack_icon))
                .setContent(new Intent(this, TypingBattleTimeAttackListActivity.class)));*/
        tabHost.addTab(tabHost.newTabSpec("tab4")
                .setIndicator("", getResources().getDrawable(R.drawable.setting_icon))
                .setContent(new Intent(this, TypingBattleMemberUpdateActivity.class)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        
        conn=new ServiceConnection() {
			ITypingBattlePollingService mService;
        	@Override
        	public void onServiceConnected(ComponentName name, IBinder service){  
        		// TODO Auto-generated catch block
        		mService =  ITypingBattlePollingService.Stub.asInterface(service);
        		try {        			
        			
        			mService.startChatMessageSync(mgr.getData(PreferenceMgr.USER_EMAIL_KEY, ""), "");       			
				} catch (RemoteException e) {					
					e.printStackTrace();
				}
        	}
        	@Override
        	public void onServiceDisconnected(ComponentName name){
        		// TODO Auto-generated catch block
        		if(mService == null) return;
        		try {        			
        			
        			mService.stopChatMessageSync();       			
				} catch (RemoteException e) {					
					e.printStackTrace();
				}
        	}			
        };
        bindService(new Intent(this,TypingBattlePollingService.class) ,conn, Context.BIND_AUTO_CREATE);
		
    }
    
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		
		this.unbindService(conn);
		super.onDestroy();
	}
}

